﻿var lifecycle = {};

(function () {

    "use strict";

    // public functions

    lifecycle.process = function () {

        initStores();

        var localSettings = Windows.Storage.ApplicationData.current.localSettings.values;

        if (!localSettings['firstInstall']) {
            localSettings['firstInstall'] = true;
            firstInstall();
        } else {
            var _runningVersion = Utils.string2version(localSettings['runningVersion']);
            var _currentVersion = Windows.ApplicationModel.Package.current.id.version;

            if (_currentVersion.major > _runningVersion.major ||
                _currentVersion.minor > _runningVersion.minor ||
                _currentVersion.build > _runningVersion.build ||
                _currentVersion.revision > _runningVersion.revision) {
                localSettings['runningVersion'] = Utils.version2string(_currentVersion);
                newVersionFirstLaunch();
            }
        }

        Windows.UI.WebUI.WebUIApplication.addEventListener("suspending", pause, false);
        Windows.UI.WebUI.WebUIApplication.addEventListener("resuming", resume, false);

        appStart();
    };

    // private properties

    var defaultList = [{
        name: 'going out',
        items: ['milk/formula', 'baby food', 'bib', 'bowl and spoon', 'soft tissues', 'wipes', 'diapers', 'sunblock', 'pacifier', 'water', 'lotion']
    }, {
        name: 'travel',
        items: ['milk/formula', 'baby food', 'bib', 'bowl and spoon', 'soft tissues', 'wipes', 'diapers', 'sunblock', 'pacifier', 'water', 'lotion', 'favorite toy', 'blanket', 'nail clip']
    }, {
        name: 'doctor\'s visit',
        items: ['immunization card', 'insurance card', 'diapers', 'wipes', 'milk/formula', 'bib', 'pacifier', 'water']
    }];

    // private functions

    function firstInstall() {
        console.log('firstInstall');
        initDefaulList();
    }

    function newVersionFirstLaunch() {
        console.log('newVersionFirstLaunch');
    }

    function appStart() {
        Utils.initialize();
        Utils.log('appStart');
        //$('.x-button-aawe-image').live('mousedown', function () {
        //    $(this).addClass('x-button-pressed');
        //});
    }

    function pause() {
        console.log('pause');
    }

    function resume() {
        console.log('resume');
    }

    function initStores() {
        (new AAWE.App.Profile.Store.ChildrenStore({ model: 'AAWE.App.Profile.Model.Child' })).load();
        (new AAWE.App.Context.Store.ContextStore({ model: 'AAWE.App.Context.Model.Context' })).load();
        (new AAWE.App.List.Store.CheckListStore({ model: 'AAWE.App.List.Model.CheckList' })).load();
        (new AAWE.App.List.Store.CheckListItemStore({ model: 'AAWE.App.List.Model.CheckListItem' })).load();
    }

    function initDefaulList() {
        var _listStore = Ext.StoreMgr.lookup('AAWE.App.List.Store.CheckListStore');
        var _listItemStore = Ext.StoreMgr.lookup('AAWE.App.List.Store.CheckListItemStore');

        var _list, _item, _id, i, j, _temp;
        for (i = 0; i < defaultList.length; i++) {
            _list = defaultList[i];
            _temp = _listStore.add({ name: _list.name, editing: false });
            _id = _temp[0].data.id;
            for (j = 0; j < _list.items.length; j++) {
                _item = _list.items[j];
                _listItemStore.add({ itemname: _item, listId: _id, isChecked: false, editing: false });
            }
        }
        _listStore.sync();
    }
})();